using System;
using System.Collections.Generic;
using System.Text;
using BIUtilities.Publish.CustomConfiguration;

namespace BIUtilities.Publish
{
    /// <summary>
    /// Executes the operations for deploying into PAS, PPS and other target environments.
    /// </summary>
    public class DeploymentManager
    {
        /// <summary>
        /// Process the deployment list of actions acording to a deployment configuration.
        /// </summary>
        /// <param name="deployConfig"></param>
        public void DeployConfiguration(DeploymentElement deployConfig)
        {
            if (deployConfig.IsEnabled)
            {
                // Update the actual Proclarity Analytic Server Endpoint with PAS report changes
                foreach (PasElement pasElement in deployConfig.PasSources)
                {
                    if (pasElement.IsEnabled)
                    {
                        System.Diagnostics.Trace.WriteLine("Setting up for PAS Updates...");
                        System.Diagnostics.Trace.Indent();

                        System.Diagnostics.Trace.WriteLine("Configuring ...");
                        System.Diagnostics.Trace.Indent();
                        ProclarityAnalyticServerDeployUtility pasDeploy = new ProclarityAnalyticServerDeployUtility();
                        pasDeploy.TargetWebserviceUrl = pasElement.TargetWebservice;
                        pasDeploy.MatchOnLibrary = pasElement.MatchOnLibrary;
                        pasDeploy.MatchOnBook = pasElement.MatchOnBook;
                        pasDeploy.MatchOnPage = pasElement.MatchOnPage;
                        pasDeploy.LoadPasMatchedItems();
                        System.Diagnostics.Trace.Unindent();

                        System.Diagnostics.Trace.WriteLine("Performing Updates...");
                        System.Diagnostics.Trace.Indent();
                        foreach (PasDatasourceElement dsElements in pasElement.PasSources)
                        {
                            if (dsElements.IsEnabled)
                            {
                                System.Diagnostics.Trace.WriteLine("Update Data Source...");
                                System.Diagnostics.Trace.Indent();
                                pasDeploy.UpdateDataSource(dsElements.OriginalServer, dsElements.NewServer);
                                System.Diagnostics.Trace.Unindent();
                            }
                        }
                        System.Diagnostics.Trace.Unindent();

                        System.Diagnostics.Trace.WriteLine("Publishing changes to target server...");
                        System.Diagnostics.Trace.Indent();
                        pasDeploy.PersistPasMatchedItems();
                        System.Diagnostics.Trace.Unindent();

                        System.Diagnostics.Trace.Unindent();
                    }
                }

                // Update the actual Excel files at a target location with changes
                foreach (ExcelReportingElement excelElement in deployConfig.ExcelReportSources)
                {
                    if (excelElement.IsEnabled)
                    {
                        System.Diagnostics.Trace.WriteLine("Setting up for Excel Reporting Updates...");
                        System.Diagnostics.Trace.Indent();

                        OfficeFileDeployUtility officeDeploy = new OfficeFileDeployUtility();
                        officeDeploy.SourceFile = excelElement.SourceExcelFileUrl;
                        officeDeploy.TargetFile = excelElement.TargetExcelFileUrl;

                        System.Diagnostics.Trace.WriteLine("Loading Source Data...");
                        System.Diagnostics.Trace.Indent();
                        officeDeploy.LoadWebFile();
                        System.Diagnostics.Trace.Unindent();

                        System.Diagnostics.Trace.WriteLine("Performing Updates...");
                        System.Diagnostics.Trace.Indent();
                        
                        foreach (ExcelReportingOlapConnectionElement excelOlapConnectionElement in excelElement.OlapConnectionUpdates)
                        {
                            if (excelOlapConnectionElement.IsEnabled)
                            {
                                System.Diagnostics.Trace.WriteLine("Update Connection...");
                                System.Diagnostics.Trace.Indent();
                                officeDeploy.UpdateDataConnection(
                                    excelOlapConnectionElement.ConnectionName,
                                    excelOlapConnectionElement.OfficeDataConnectionUrl);
                                System.Diagnostics.Trace.Unindent();
                            }
                        }

                        foreach (ExcelReportingPasLookupElement excelPasLookupElement in excelElement.PasLookupUpdates)
                        {
                            if (excelPasLookupElement.IsEnabled)
                            {
                                System.Diagnostics.Trace.WriteLine("Update Lookup Sheet for Proclarity Analytic Server Links...");
                                System.Diagnostics.Trace.Indent();
                                officeDeploy.UpdatePasLookups(
                                    excelPasLookupElement.PasServerName,
                                    excelPasLookupElement.SheetName,
                                    excelPasLookupElement.MappingPasPathColumn,
                                    excelPasLookupElement.MappingPasLibraryIdColumn,
                                    excelPasLookupElement.MappingPasBookIdColumn,
                                    excelPasLookupElement.MappingPasPageIdColumn,
                                    excelPasLookupElement.MappingPasServerNameColumn);
                                System.Diagnostics.Trace.Unindent();
                            }
                        }



                        System.Diagnostics.Trace.Unindent();

                        System.Diagnostics.Trace.WriteLine("Publishing changes to target server...");
                        System.Diagnostics.Trace.Indent();
                        officeDeploy.PersistWebFile();
                        System.Diagnostics.Trace.Unindent();

                        System.Diagnostics.Trace.Unindent();
                    }
                }

                // Update a target PPS site with changes affected after loading from a source location.
                foreach (PerformancePointElement ppsElement in deployConfig.PerformancePointSources)
                {
                    if (ppsElement.IsEnabled)
                    {
                        System.Diagnostics.Trace.WriteLine("Setting up for Pps Updates...");
                        System.Diagnostics.Trace.Indent();

                        PerformancePointDeployUtility ppsDeploy = new PerformancePointDeployUtility();
                        if (ppsElement.SourceWorkspace.Length == 0)
                            ppsDeploy.SourceWebserviceUrl = ppsElement.SourceMonitoringWebservice;
                        else
                            ppsDeploy.SourceWorkspace = ppsElement.SourceWorkspace;
                        ppsDeploy.TargetWebserviceUrl = ppsElement.TargetMonitoringWebservice;
                        ppsDeploy.MatchOnProperty = ppsElement.MatchOnProperty;
                        ppsDeploy.MatchOnValue = ppsElement.MatchOnValue;

                        // Make sure the connections are working
                        System.Diagnostics.Trace.WriteLine("Testing the Connection...");
                        System.Diagnostics.Trace.Indent();
                        ppsDeploy.VerifySourceConnection();
                        ppsDeploy.VerifyTargetConnection();
                        System.Diagnostics.Trace.Unindent();

                        // Load all of the elements into cache
                        System.Diagnostics.Trace.WriteLine("Loading Source Data...");
                        System.Diagnostics.Trace.Indent();
                        ppsDeploy.LoadPpsMatchedItems();
                        System.Diagnostics.Trace.Unindent();

                        System.Diagnostics.Trace.WriteLine("Performing Updates...");
                        System.Diagnostics.Trace.Indent();
                        foreach (PerformancePointSecurityElement secElements in ppsElement.SecurityChanges)
                        {
                            if (secElements.IsEnabled)
                            {
                                System.Diagnostics.Trace.WriteLine("Updating Credentials...");
                                System.Diagnostics.Trace.Indent();
                                ppsDeploy.UpdateSecurity(secElements.Credential, secElements.Role, secElements.Action);
                                System.Diagnostics.Trace.Unindent();
                            }
                        }

                        foreach (PerformancePointDataSourceElement dsElements in ppsElement.DataSourceChanges)
                        {
                            if (dsElements.IsEnabled)
                            {
                                System.Diagnostics.Trace.WriteLine("Updating Data Sources...");
                                System.Diagnostics.Trace.Indent();
                                ppsDeploy.UpdateDataSource(dsElements.SourceName, dsElements.FullyQualifiedServername);
                                System.Diagnostics.Trace.Unindent();
                            }
                        }

                        if (ppsElement.ExcelReportChanges.IsEnabled)
                        {
                            foreach (PerformancePointExcelReportSitesElement excelSiteElements in ppsElement.ExcelReportChanges.SiteChanges)
                            {
                                if (excelSiteElements.IsEnabled)
                                {
                                    System.Diagnostics.Trace.WriteLine("Updating Excel Reports...");
                                    System.Diagnostics.Trace.Indent();
                                    ppsDeploy.UpdateExcelReportLocation(
                                        excelSiteElements.OriginalSharepointSite, excelSiteElements.NewSharepointSite,
                                        excelSiteElements.OriginalSharepointLibrary, excelSiteElements.NewSharepointLibrary);
                                    System.Diagnostics.Trace.Unindent();
                                }
                            }
                        }

                        foreach (PerformancePointExcelReportParamElement excelParamElements in ppsElement.ExcelReportChanges.ParameterChanges)
                        {
                            if (excelParamElements.IsEnabled)
                            {
                                System.Diagnostics.Trace.WriteLine("Updating Excel Parameters...");
                                System.Diagnostics.Trace.Indent();
                                ppsDeploy.UpdateExcelReportParameters(excelParamElements.ParamName, excelParamElements.NewParamValue);
                                System.Diagnostics.Trace.Unindent();
                            }
                        }

                        if (ppsElement.PasReportChanges.IsEnabled)
                        {
                            foreach (PerformancePointPasReportSitesElement pasSite in ppsElement.PasReportChanges.SiteChanges)
                            {
                                if (pasSite.IsEnabled)
                                {
                                    System.Diagnostics.Trace.WriteLine("Updating Proclarity Analytic Server Reports...");
                                    System.Diagnostics.Trace.Indent();
                                    ppsDeploy.UpdatePasReportSites(pasSite.OriginalPasSite, pasSite.NewPasSite);
                                    System.Diagnostics.Trace.Unindent();
                                }
                            }
                        }

                        if (ppsElement.WebPageChanges.IsEnabled)
                        {
                            foreach (PerformancePointWebPageUrlReplaceElement webUrlReplacement in ppsElement.WebPageChanges.UrlReplacements)
                            {
                                if (webUrlReplacement.IsEnabled)
                                {
                                    System.Diagnostics.Trace.WriteLine("Updating Web Page URLs Reports...");
                                    System.Diagnostics.Trace.Indent();
                                    ppsDeploy.UpdateWebPageUrlReplacements(webUrlReplacement.FindString, webUrlReplacement.ReplaceFoundString);
                                    System.Diagnostics.Trace.Unindent();
                                }
                            }
                        }

                        if (ppsElement.SsrsReportChanges.IsEnabled)
                        {
                            foreach (PerformancePointSsrsReportSitesElement ssrsSiteReplacement in ppsElement.SsrsReportChanges.SiteChanges)
                            {
                                if (ssrsSiteReplacement.IsEnabled)
                                {
                                    System.Diagnostics.Trace.WriteLine("Updating SSRS Reports...");
                                    System.Diagnostics.Trace.Indent();
                                    ppsDeploy.UpdateSsrsReportSites(ssrsSiteReplacement.OriginalSsrsSite, ssrsSiteReplacement.NewSsrsSite);
                                    System.Diagnostics.Trace.Unindent();
                                }
                            }

                            foreach (PerformancePointSsrsReportPathReplaceElement ssrsPathReplacement in ppsElement.SsrsReportChanges.PathReplacements)
                            {
                                if (ssrsPathReplacement.IsEnabled)
                                {
                                    System.Diagnostics.Trace.WriteLine("Updating SSRS Reports...");
                                    System.Diagnostics.Trace.Indent();
                                    ppsDeploy.UpdateSsrsReportPaths(ssrsPathReplacement.FindPath, ssrsPathReplacement.ReplaceFoundPath);
                                    System.Diagnostics.Trace.Unindent();
                                }
                            }
                        }

                        System.Diagnostics.Trace.Unindent();

                        System.Diagnostics.Trace.WriteLine("Publishing changes to target server...");
                        System.Diagnostics.Trace.Indent();
                        ppsDeploy.PersistPpsMatchedItems();
                        System.Diagnostics.Trace.Unindent();

                        System.Diagnostics.Trace.Unindent();
                    }
                }


            }

        }
    }
}
